﻿using FieldTools.Util.Helper;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FieldTools.Client.View.Window
{
    /// <summary>
    /// ConsoleWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ConsoleWindow : MahApps.Metro.Controls.MetroWindow
    {
        public ConsoleWindow()
        {
            InitializeComponent();

            //consolePanel是父容器名
            int width = (int)(this.Width - 0);//留出边框
            int height = (int)(this.Height - 0);
            //获取winform容器控件句柄
            var hwnd = consolePanel.Handle;
            //设置容器基础属性，最小尺寸务必设置，否者窗口无效
            consolePanel.BackColor = System.Drawing.Color.AliceBlue;
            consolePanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            consolePanel.Height = height;
            consolePanel.Width = width;
            //consolePanel.MaximumSize = new System.Drawing.Size(width, height);
            consolePanel.MinimumSize = new System.Drawing.Size(width, height);

            //获取控制台句柄
            var consolePtr = InitConsole();

            //hwnd是父容器句柄，consolePtr是控制台句柄
            _ = User32Helper.SetParent(consolePtr, hwnd);      //将第三方窗体嵌入父容器                    
            Thread.Sleep(100);                      //略加延时
            _ = User32Helper.ShowWindow(consolePtr, 3);                 //让第三方窗体在容器中最大化显示
            RemoveWindowTitle(consolePtr);             // 去除窗体标题

            this.SizeChanged += (sender, e) =>
            {
                //textBlock.Text = $"尺寸为：({e.NewSize.Width},{ e.NewSize.Height})";
                //这里留出20是为保留些边框，实际情况根据自己需求来
                _ = User32Helper.MoveWindow(consolePtr, 0, 0, (int)e.NewSize.Width - 0, (int)e.NewSize.Height - 0, true);
            };
        }

        /// <summary>
        /// 初始化控制台
        /// </summary>
        public static nint InitConsole()
        {
            if (!HasConsole)
            {
                ShowConsole();
                Log.Information("控制台初始化成功");
            }
            var consolePtr = Kernel32Helper.GetConsoleWindow();
            //隐藏控制台，避免闪烁
            _ = User32Helper.ShowWindow(consolePtr, 0);
            return consolePtr;
        }

        /// <summary>
        /// 去除窗体标题（可选，会让窗口嵌入更完美，尤其是本例中的控制台）
        /// </summary>
        /// <param name="vHandle">窗口句柄</param>
        public static void RemoveWindowTitle(IntPtr vHandle)
        {
            long style = User32Helper.GetWindowLong(vHandle, -16);
            style &= ~0x00C00000;
            User32Helper.SetWindowLong(vHandle, -16, style);
        }

        /// <summary>
        /// 控制台是否存在
        /// </summary>
        public static bool HasConsole => Kernel32Helper.GetConsoleWindow() != IntPtr.Zero;

        /// <summary>
        /// 显示控制台
        /// </summary>
        public static void ShowConsole()
        {
            if (!HasConsole)
            {
                Kernel32Helper.AllocConsole();
            }
        }
    }
}
